# -*- coding: utf-8 *-*

#   This file is part of Sql-Rice.
#
#    Sql-Rice is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    Sql-Rice is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Sql-Rice; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

import unittest
import sql_parse


class TestSentence(unittest.TestCase):

    def test_fields(self):
        sentence = sql_parse.Sentence('SELECT rut,nombre FROM clientes')
        self.assertListEqual(['rut', 'nombre'], sentence.fields)

    def test_table(self):
        sentence = sql_parse.Sentence('SELECT rut, nombre, FROM clientes')
        self.assertListEqual(['clientes'], sentence.tables)

    def test_params(self):
        sentence = sql_parse.Sentence('SELECT rut FROM clientes WHERE' + \
                                      ' rut=:rut')
        self.assertListEqual(['rut'], sentence.params)

    def test_params_unique(self):
        sentence = sql_parse.Sentence('SELECT rut, nombre FROM clientes ' + \
                                      'WHERE rut=:rut OR (rut=:rut AND ' + \
                                      'nombre=:nombre )')
        assert len(sentence.params) == 2

    def test_make_update(self):
        sentence = sql_parse.Sentence('SELECT rut, nombre, FROM clientes')
        self.assertEqual(sentence.update_query,
                         'UPDATE clientes SET rut=:rut, nombre=:nombre ' +
                         'WHERE rut=:old_rut AND nombre=:old_nombre')

    def test_make_insert(self):
        sentence = sql_parse.Sentence('SELECT rut, nombre, FROM clientes')
        self.assertEqual(sentence.insert_query,
                        'INSERT INTO clientes(rut, nombre)' +
                        ' VALUES(:rut, :nombre)')

    def test_make_delete(self):
        sentence = sql_parse.Sentence('SELECT rut, nombre, FROM clientes')
        self.assertEqual(sentence.delete_query,
                        'DELETE FROM clientes WHERE rut=:rut ' +
                        'AND nombre=:nombre')

    def test_no_make(self):
        s = sql_parse.Sentence('SELECT rut, nombre FROM clientes',
                            generate_insert=False, generate_update=False,
                            generate_delete=False)
        self.assertEqual(s.insert_query, '')
        self.assertEqual(s.update_query, '')
        self.assertEqual(s.delete_query, '')

